/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;
import com.jnzc.ocr.common.utils.BusinessUtils;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.serv.shop.entity.ShopMain;
import com.jnzc.ocr.serv.shop.entity.ShopUser;
import com.jnzc.ocr.serv.shop.entity.ShopUserRela;
import com.jnzc.ocr.serv.shop.mapper.ShopUserMapper;
import com.jnzc.ocr.serv.shop.mapper.ShopUserRelaMapper;
import com.jnzc.ocr.serv.shop.service.ShopUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangcm
 * @version 1.0, 2017/8/31
 */
@Service
public class ShopUserServiceImpl implements ShopUserService {

    @Autowired
    private ShopUserMapper shopUserMapper;

    @Autowired
    private ShopUserRelaMapper shopUserRelaMapper;

    @Override
    public ShopUser find(Long userId) {
        ValidateUtils.isId(userId, "用户ID");
        ShopUser obj = shopUserMapper.selectById(userId);
        BusinessUtils.notNull(obj, "店铺用户信息不存在");
        return obj;
    }

    @Override
    public List<ShopUserRela> listShopRela(Long userId) {
        ValidateUtils.isId(userId, "用户ID");
        ShopUserRela filter = new ShopUserRela();
        filter.setUserId(userId);
        EntityWrapper<ShopUserRela> ew = new EntityWrapper<ShopUserRela>(filter);
        ew.orderBy("id");
        return shopUserRelaMapper.selectList(ew);
    }

    public void insert(ShopUser shopUser, List<ShopMain> shopMainList) {
        shopUser.setCreateTime(DateUtils.getNow());
        shopUserMapper.insert(shopUser);
        if(CollectionUtils.isNotEmpty(shopMainList)){
            for (ShopMain shopMain : shopMainList) {
                ShopUserRela obj = new ShopUserRela();
                obj.setUserId(shopUser.getUserId());
                obj.setShopId(shopMain.getId());
                shopUserRelaMapper.insert(obj);
            }
        }
    }

    @Override
    public void update(ShopUser obj) {
        shopUserMapper.updateById(obj);
    }
}
